package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.system.domain.vo.PhenologicalperiodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Phenologicalguidance;
import com.ruoyi.system.mapper.PhenologicalperiodsMapper;
import com.ruoyi.system.domain.Phenologicalperiods;
import com.ruoyi.system.service.IPhenologicalperiodsService;

/**
 * 作物物候期管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-24
 */
@Service
public class PhenologicalperiodsServiceImpl implements IPhenologicalperiodsService 
{
    @Autowired
    private PhenologicalperiodsMapper phenologicalperiodsMapper;

    /**
     * 查询作物物候期管理
     * 
     * @param phenologicalperiodid 作物物候期管理主键
     * @return 作物物候期管理
     */
    @Override
    public Phenologicalperiods selectPhenologicalperiodsByPhenologicalperiodid(Long phenologicalperiodid)
    {
        return phenologicalperiodsMapper.selectPhenologicalperiodsByPhenologicalperiodid(phenologicalperiodid);
    }

    /**
     * 查询作物物候期管理列表
     * 
     * @param phenologicalperiods 作物物候期管理
     * @return 作物物候期管理
     */
    @Override
    public List<Phenologicalperiods> selectPhenologicalperiodsList(Phenologicalperiods phenologicalperiods)
    {
        return phenologicalperiodsMapper.selectPhenologicalperiodsList(phenologicalperiods);
    }

    /**
     * 新增作物物候期管理
     * 
     * @param phenologicalperiods 作物物候期管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPhenologicalperiods(Phenologicalperiods phenologicalperiods)
    {
        int rows = phenologicalperiodsMapper.insertPhenologicalperiods(phenologicalperiods);
        insertPhenologicalguidance(phenologicalperiods);
        return rows;
    }

    /**
     * 修改作物物候期管理
     * 
     * @param phenologicalperiods 作物物候期管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePhenologicalperiods(Phenologicalperiods phenologicalperiods)
    {
        phenologicalperiodsMapper.deletePhenologicalguidanceByPhenologicalperiodid(phenologicalperiods.getPhenologicalperiodid());
        insertPhenologicalguidance(phenologicalperiods);
        return phenologicalperiodsMapper.updatePhenologicalperiods(phenologicalperiods);
    }

    /**
     * 批量删除作物物候期管理
     * 
     * @param phenologicalperiodids 需要删除的作物物候期管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePhenologicalperiodsByPhenologicalperiodids(Long[] phenologicalperiodids)
    {
        phenologicalperiodsMapper.deletePhenologicalguidanceByPhenologicalperiodids(phenologicalperiodids);
        return phenologicalperiodsMapper.deletePhenologicalperiodsByPhenologicalperiodids(phenologicalperiodids);
    }

    /**
     * 删除作物物候期管理信息
     * 
     * @param phenologicalperiodid 作物物候期管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePhenologicalperiodsByPhenologicalperiodid(Long phenologicalperiodid)
    {
        phenologicalperiodsMapper.deletePhenologicalguidanceByPhenologicalperiodid(phenologicalperiodid);
        return phenologicalperiodsMapper.deletePhenologicalperiodsByPhenologicalperiodid(phenologicalperiodid);
    }

    @Override
    public List<PhenologicalperiodsVo> selectPhenologicalperiodsListVo(Phenologicalperiods phenologicalperiods) {
//        return Collections.emptyList();
        return phenologicalperiodsMapper.selectPhenologicalperiodsListVo(phenologicalperiods);
    }

//    @Override
//    public List<PhenologicalperiodsVo> selectPhenologicalperiodsListVo(Phenologicalperiods phenologicalperiods) {
//        return phenologicalperiodsMapper.selectPhenologicalperiodsListVo(phenologicalperiods);
//    }

    /**
     * 新增标准化生产指导信息信息
     * 
     * @param phenologicalperiods 作物物候期管理对象
     */
    public void insertPhenologicalguidance(Phenologicalperiods phenologicalperiods)
    {
        List<Phenologicalguidance> phenologicalguidanceList = phenologicalperiods.getPhenologicalguidanceList();
        Long phenologicalperiodid = phenologicalperiods.getPhenologicalperiodid();
        if (StringUtils.isNotNull(phenologicalguidanceList))
        {
            List<Phenologicalguidance> list = new ArrayList<Phenologicalguidance>();
            for (Phenologicalguidance phenologicalguidance : phenologicalguidanceList)
            {
                phenologicalguidance.setPhenologicalperiodid(phenologicalperiodid);
                list.add(phenologicalguidance);
            }
            if (list.size() > 0)
            {
                phenologicalperiodsMapper.batchPhenologicalguidance(list);
            }
        }
    }
}
